class Sales::FollowUpsController < ApplicationController
  before_action :set_sales_follow_up, only: [:show, :edit, :update, :destroy]


  def calendar
    @html_title = '跟进日历'
    # 加载full-calendar的js全家桶
    @need_calendar = true

    @follow_ups = Sales::FollowUp.where(
        user_id: current_user.id, appointment_time: Time.now.at_beginning_of_month..Time.now.end_of_month
    ).includes(:firm)
    @follow_ups = @follow_ups.where(completed_at: nil) if params[:view_type].to_i==2
    @follow_ups = @follow_ups.map(&:calendar_attr)
  end

  # GET /sales/follow_ups
  # GET /sales/follow_ups.json
  def index
    @sales_follow_ups = Sales::FollowUp.all
  end

  # GET /sales/follow_ups/1
  # GET /sales/follow_ups/1.json
  def show
    @contacts = Sales::Contact.where(firm_id: @sales_follow_up.firm_id)
    render layout: 'modal'
  end

  # GET /sales/follow_ups/new
  def new
    @html_title = '新增跟进记录'
    @sales_follow_up = Sales::FollowUp.new(firm_id: params[:firm_id], contact_id: params[:contact_id])
    firm = @sales_follow_up.firm
    @contacts = Sales::Contact.where(firm_id: params[:firm_id]).pluck(:name,:id)
    @sales_follow_up.leads_type = firm.leads.find{ |l|l.user_id==current_user.id }.try(:leads_type) || ::Sales::FirmLead::LEADS_TYPE[:main]
    @sales_follow_up.user_id = current_user.id
    render layout: 'modal'
  end

  # GET /sales/follow_ups/1/edit
  def edit
    @html_title = '修改跟进信息'
    @contacts = Sales::Contact.where(firm_id: @sales_follow_up.firm_id).pluck(:name,:id)
    render layout: 'modal'
  end

  # POST /sales/follow_ups
  # POST /sales/follow_ups.json
  def create
    @sales_follow_up = Sales::FollowUp.new(sales_follow_up_params)

    respond_to do |format|
      if @sales_follow_up.save
        format.html { redirect_to @sales_follow_up.firm, notice: '任务添加成功！' }
        format.json { render :show, status: :created, location: @sales_follow_up }
      else
        format.html { render :new }
        format.json { render json: @sales_follow_up.errors, status: :unprocessable_entity }
        format.js{
          show_js_notice @sales_follow_up, type: :error
        }
      end
    end
  end

  # PATCH/PUT /sales/follow_ups/1
  # PATCH/PUT /sales/follow_ups/1.json
  def update
    respond_to do |format|
      if @sales_follow_up.update(sales_follow_up_params)
        format.html { redirect_to @sales_follow_up.firm, notice: '保存成功！' }
        format.json { render json: @sales_follow_up }
      else
        format.html { render :edit }
        format.json { render json: @sales_follow_up.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /sales/follow_ups/1
  # DELETE /sales/follow_ups/1.json
  def destroy
    @sales_follow_up.destroy
    respond_to do |format|
      format.html { redirect_to sales_follow_ups_url, notice: 'Follow up was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_sales_follow_up
      @sales_follow_up = Sales::FollowUp.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def sales_follow_up_params
      params.require(:sales_follow_up).permit(
          :firm_id, :contact_id, :leads_type, :follow_up_type,
          :appointment_time, :completed_at, :content, :user_id,
          :created_by, :completed_by, :follow_up_target_type, :is_completed
      )
    end
end
